﻿using Neural;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Collections;

namespace NNTest
{
    
    
    /// <summary>
    ///This is a test class for EvaluatorTest and is intended
    ///to contain all EvaluatorTest Unit Tests
    ///</summary>
    [TestClass()]
    public class EvaluatorTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for ErrorNames
        ///</summary>
        [TestMethod()]
        public void ErrorNamesTest()
        {
            Evaluator target = new Evaluator(); // TODO: Initialize to an appropriate value
            Assert.IsTrue(target.ErrorNames.Count == 0);

            target.AddErrorCalculator(new Neural.Calculators.SSEErrorCalculator(), "SSE");
            Assert.IsTrue(target.ErrorNames.Contains("SSE"));
        }

        /// <summary>
        ///A test for GetError
        ///</summary>
        [TestMethod()]
        public void GetErrorTest()
        {
            Evaluator target = new Evaluator(); // TODO: Initialize to an appropriate value
            target.AddErrorCalculator(new Neural.Calculators.SSEErrorCalculator(), "SSE");
            string name = "SSE"; // TODO: Initialize to an appropriate value
            double expected = 0.0;
            double actual;
            actual = target.GetError(name);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for Evaluate
        ///</summary>
        [TestMethod()]
        public void EvaluateTest()
        {
            Evaluator target = new Evaluator(); // TODO: Initialize to an appropriate value
            target.AddErrorCalculator(new Neural.Calculators.SSEErrorCalculator(), "SSE");
            Network network = BuildTestNetwork(); // TODO: Initialize to an appropriate value
            ExampleSet examples = BuildTestExampleSet(); // TODO: Initialize to an appropriate value
            Dictionary<System.String, double> expected = new Dictionary<System.String, double>(); // TODO: Initialize to an appropriate value
            expected["SSE"] = 0.0;
            Dictionary<System.String, double> actual = target.Evaluate(network, examples);
            Assert.AreEqual(expected["SSE"], actual["SSE"], 0.001);
            
        }

        /// <summary>
        ///A test for AddErrorCalculator
        ///</summary>
        [TestMethod()]
        public void AddErrorCalculatorTest()
        {
            Evaluator target = new Evaluator(); // TODO: Initialize to an appropriate value
            ErrorCalculator ec = new Neural.Calculators.SSEErrorCalculator(); // TODO: Initialize to an appropriate value
            target.AddErrorCalculator(ec, "SSE");
            Assert.IsTrue(target.ErrorNames.Contains("SSE"));
        }

        private Network BuildTestNetwork()
        {
            Network n = new Network();
            n.AddInputNeuron(new Neuron("Foo"));
            n.AddOutputNeuron(new Neuron("Bar"));
            n.Connect("Foo", "Bar");

            n["Bar"].Weights = new double[] { 0.0, 0.0 };
            return n;
        }

        private ExampleSet BuildTestExampleSet()
        {
            ExampleSet result = new ExampleSet();

            for (int i = 0; i < 4; i++)
            {
                Example ex = new Example();
                ex.Expected["Bar"] = 0.5;
                ex.Inputs["Foo"] = 0.1;

                result.AddExample(ex);
            }

            return result;
        }

        /// <summary>
        ///A test for Accumulate
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Neural.NET.dll")]
        public void AccumulateTest()
        {
            Evaluator target = new Evaluator();
            target.AddErrorCalculator(new Neural.Calculators.SSEErrorCalculator(), "SSE");
            target.Evaluate(BuildTestNetwork(), BuildTestExampleSet());
            Assert.AreEqual(target.GetError("SSE"), 0.0, 0.0001);
        }

        /// <summary>
        ///A test for Evaluator Constructor
        ///</summary>
        [TestMethod()]
        public void EvaluatorConstructorTest()
        {
            Evaluator target = new Evaluator();
            Assert.IsNotNull(target);
            Assert.AreEqual(target.ErrorNames.Count, 0);
        }
    }
}
